from datetime import datetime
from django.contrib import admin
from django.core.files.base import ContentFile
from django.utils.html import format_html

from .models import Diy, Ware, Like, Collect, Browse
from system.wechat import CustomStorage

from wechatpy import WeChatClient
from decouple import config


# Register your models here.
@admin.action(description="重新生成二维码")
def gen_weapp_qrcode(modeladmin, request, queryset):
    wx_client = WeChatClient(config('WEIXIN_APP_ID'), config('WEIXIN_APP_SECRET'), session=CustomStorage())
    for diy in queryset:
        try:
            response = wx_client.wxa.get_wxa_code_unlimited(
                    'id=' + diy.uuid,
                    page='pages/detail/detail', 
                )
            diy.weapp_qrcode.save(f'share_{diy.uuid}.png', ContentFile(response.content))
            diy.save()
        except Exception as e:
            print('生成二维码失败', diy.uuid, str(e))

@admin.register(Diy)
class DiyAdmin(admin.ModelAdmin):
    list_display = ['pk', 'account', 'uuid', 'title', 'price', 'get_weapp_qrcode', 'remark', 'create_time']

    search_fields = ['uuid', 'title', 'desc','remark']

    actions = [gen_weapp_qrcode]

    fieldsets = [
        (
            None,
            {
                "fields": [
                    "account", 
                    "title", 
                    'desc',
                    'price',
                ],
            },
        ),
        (
            "额外选项",
            {
                "classes": ["collapse"],
                "fields": ["remark", "enable"],
            },
        ),
    ]

    def get_weapp_qrcode(self, obj):
        if obj.weapp_qrcode:
            return format_html('<img src="{}" width="100px" />', obj.weapp_qrcode.url)
        return "暂无图片"
    get_weapp_qrcode.short_description = "小程序码"

@admin.register(Ware)
class WareAdmin(admin.ModelAdmin):
    list_display = ['pk', 'diy', 'title', 'price', 'count', 'brand', 'remark', 'create_time']

    fieldsets = [
        (
            None,
            {
                "fields": [
                    "diy", 
                    'title',
                    'desc',
                    'price',
                    'count',
                    'brand',
                    'image'
                ],
            },
        ),
        (
            "额外选项",
            {
                "classes": ["collapse"],
                "fields": ["remark", "enable"],
            },
        ),
    ]


@admin.register(Like)
class LikeAdmin(admin.ModelAdmin):
    list_display = ['pk', 'account', 'diy', 'remark', 'create_time']

    fieldsets = [
        (
            None,
            {
                "fields": [
                    "account", 
                    "diy"
                ],
            },
        ),
        (
            "额外选项",
            {
                "classes": ["collapse"],
                "fields": ["remark", "enable"],
            },
        ),
    ]


@admin.register(Collect)
class CollectAdmin(admin.ModelAdmin):
    list_display = ['pk', 'account', 'diy', 'remark', 'create_time']

    fieldsets = [
        (
            None,
            {
                "fields": [
                    "account", 
                    "diy"
                ],
            },
        ),
        (
            "额外选项",
            {
                "classes": ["collapse"],
                "fields": ["remark", "enable"],
            },
        ),
    ]
